package com.xjrsoft.module.visual.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.base.entity.XjrBaseModule;
import com.xjrsoft.module.base.service.IXjrBaseModuleService;
import com.xjrsoft.module.visual.dto.VisualReleaseDto;
import com.xjrsoft.module.visual.dto.VisualReleaseHomeDto;
import com.xjrsoft.module.visual.dto.VisualReleaseListDto;
import com.xjrsoft.module.visual.entity.XjrVisual;
import com.xjrsoft.module.visual.entity.XjrVisualRelease;
import com.xjrsoft.module.visual.mapper.XjrVisualReleaseMapper;
import com.xjrsoft.module.visual.service.IXjrVisualReleaseService;
import com.xjrsoft.module.visual.service.IXjrVisualService;
import com.xjrsoft.module.visual.vo.VisualReleaseHomeVo;
import com.xjrsoft.module.visual.vo.VisualReleaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 看板发布表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-03-10
 */
@Service
//@AllArgsConstructor
public class XjrVisualReleaseServiceImpl extends ServiceImpl<XjrVisualReleaseMapper, XjrVisualRelease> implements IXjrVisualReleaseService {


    @Autowired
    private  IXjrBaseModuleService moduleService;
     @Autowired
    private  IXjrVisualService visualService;
    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/19
     * @Param:[dto]
     * @return:com.xjrsoft.common.page.PageOutput
     * @Description:分页查询
     */
    @Override
    public PageOutput<VisualReleaseVo> selectPageList(VisualReleaseListDto dto) {
        QueryWrapper<XjrVisualRelease> query = new QueryWrapper<>();
        query.lambda().like(!StrUtil.hasBlank(dto.getKeyword()), XjrVisualRelease::getFullName, dto.getKeyword());
        IPage<XjrVisualRelease> page = this.page(ConventPage.getPage(dto), query);
        PageOutput<VisualReleaseVo> pageOutput = ConventPage.getPageOutput(page, VisualReleaseVo.class);
        return pageOutput;
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/12/19
     * @Param:[dto]
     * @return:boolean
     * @Description:更新看板发布
     */
    @Override
    public boolean updateVisualRelease(String id, VisualReleaseDto dto) {
        XjrVisualRelease visualRelease = BeanUtil.copy(dto, XjrVisualRelease.class);
        visualRelease.setId(id);
        return this.updateById(visualRelease);
    }

    @Override
    public PageOutput<VisualReleaseHomeVo> getReleaseInHomePageList(VisualReleaseHomeDto dto) {
        String keyword = dto.getKeyword();
        if (!StringUtil.isEmpty(keyword)) {
            keyword = StringPool.PERCENT + keyword + StringPool.PERCENT;
        }
        IPage<Object> page = ConventPage.getPage(dto);
        //获取发布到首页的桌面设计
        List<VisualReleaseHomeVo> resultList = this.baseMapper.getDesktopReleaseInHomePageList(keyword, page);
        return ConventPage.getPageOutput(page.getTotal(), resultList);
    }

    /**
     * 检查看板是否能发布成首页
     *
     * @param visualId
     * @param releaseId
     * @return
     */
    @Override
    public void checkReleasedHome(String visualId, String releaseId) {
        if (this.count(Wrappers.<XjrVisualRelease>query().lambda()
                .eq(XjrVisualRelease::getKanBanId, visualId)
                .eq(XjrVisualRelease::getIndexs, 1)
                .eq(!StringUtil.isEmpty(releaseId), XjrVisualRelease::getId, releaseId)) > 0) {
            throw new RuntimeException("不能重复发布成首页!");
        }
    }

    @Override
    public boolean add(VisualReleaseDto dto) {
        XjrVisualRelease visualRelease = BeanUtil.copy(dto, XjrVisualRelease.class);
        XjrBaseModule module = BeanUtil.copy(dto, XjrBaseModule.class);
        if (StringUtil.isEmpty(module.getParentId())) {
            module.setParentId(StringPool.ZERO);
        }
        String moduleId = IdWorker.get32UUID();
        module.setIsMenu(1);
        module.setModuleId(moduleId);
        visualRelease.setModuleId(moduleId);
        String visualId = visualRelease.getKanBanId();
        boolean isSuccess = this.save(visualRelease) && moduleService.save(module);
        if (isSuccess && !visualService.isReleased(visualId)) {
            // 修改bi发布状态
            XjrVisual visual = new XjrVisual();
            visual.setId(visualId);
            visual.setStatus(1); //0-未发布，1-发布
            visualService.updateById(visual);
        }
        return isSuccess;
    }


    @Override
    public boolean updateById(XjrVisualRelease visualRelease) {
        // 检查是否可以发布
        this.checkReleasedHome(visualRelease.getId(), visualRelease.getId());
        return super.updateById(visualRelease);
    }

    @Override
    public boolean save(XjrVisualRelease visualRelease) {
        // 检查是否可以发布
        this.checkReleasedHome(visualRelease.getKanBanId(), null);
        return super.save(visualRelease);
    }

    @Override
    public boolean update(XjrVisualRelease xjrVisualRelease) {
        return super.updateById(xjrVisualRelease);
    }

    @Override
    public JSONArray getAllReleaseInHome() {
        //获取所有发布到首页的桌面设计
        JSONArray result = new JSONArray();
        List<VisualReleaseHomeVo> desktopReleaseInHomeList = this.baseMapper.getDesktopReleaseInHomePageList(null, null);
        JSONObject desktopObj = new JSONObject();
        desktopObj.put("title","桌面设计");
        desktopObj.put("urlAddress",null);
        desktopObj.put("children",desktopReleaseInHomeList);
        result.add(desktopObj);
        return result;
    }

}
